package com.example.win.android_banduyujia.peopleactivity.activity.activity.firmactivity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.Model;
import com.example.win.android_banduyujia.peopleactivity.fragment.ProTypeFragment;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;

/**
 * Created by Win on 2015/9/6.
 */
public class ListCityActivity extends BaseTitleBarActivity {

    private String[]  list;
    private TextView[]  tvList;
    private View[]      views;
    private LayoutInflater inflater;
    private ScrollView     scrollView;
    private ViewPager      viewPager;
    private int   currentItem =0 ;
    private ScopeAdapter shopAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_city_list);

        showToolsView();
        initPager();
    }

    private void showToolsView() {
        scrollView=(ScrollView)findViewById(R.id.title_scroll_view_tools);
        shopAdapter = new ScopeAdapter(getSupportFragmentManager());

        list = Model.SCOPE_CITY_BEIJING;
        LinearLayout  toolsLayout= (LinearLayout) findViewById(R.id.layout_tools);
        tvList = new TextView[list.length];
        views = new View[list.length];

        inflater = LayoutInflater.from(this);

        for(int i=0;i<list.length;i++){
            View view=inflater.inflate(R.layout.view_city_list_item, null);
            view.setId(i);
            view.setOnClickListener(toolsItemListener);
            TextView textView= (TextView) view.findViewById(R.id.text_view_list_item);
            textView.setText(list[i]);
            toolsLayout.addView(view);
            tvList[i] = textView;
            views[i] = view;
        }
        changeTextColor(0);
    }

    private View.OnClickListener toolsItemListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(v.getId());
        }
    };

    /**
     * initPager<br/>
     * ��ʼ��ViewPager�ؼ��������
     */
    private void initPager() {
        viewPager=(ViewPager)findViewById(R.id.list_view_pager);
        viewPager.setAdapter(shopAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
               if(viewPager.getCurrentItem() !=i)
                   viewPager.setCurrentItem(i);
                if(currentItem !=i){
                    changeTextColor(i);
                    changeTextLocation(i);
                }
                currentItem = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private class ScopeAdapter extends FragmentPagerAdapter {

        public ScopeAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int index) {
            Fragment fragment=new ProTypeFragment();
            Bundle  bundle = new Bundle();
            bundle.putInt("index", index);
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return list.length;
        }
    }

    /**
     * �ı�textView����ɫ
     *
     * @param id
     */
    private void changeTextColor(int id) {
        for (int i =0; i< tvList.length; i++){
            if(i !=id){
                tvList[i].setBackgroundColor(0x00000000);
                tvList[i].setTextColor(0x00000000);
            }
        }
        tvList[id].setBackgroundColor(0xffffffff);
        tvList[id].setTextColor(0xcccccccc);
    }
    /**
     * �ı���Ŀλ��
     *
     * @param i
     */
    private void changeTextLocation(int i) {
       int x=(views[i].getTop());
       scrollView.smoothScrollTo(0, x);
    }
}
